package cn.edu.ctbu.dp03.service;
import cn.edu.ctbu.dp03.dao.TeacherRepository;
import cn.edu.ctbu.dp03.entity.Teacher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TeacherService {

    @Autowired
    public TeacherRepository teacherRepository;

    public List<Teacher> getAll(){
        return teacherRepository.findAll();
    }

    /**
     * 按ID进行查询
     * @param id 主键，整数
     * @return
     */
    public Teacher findById(Integer id){


        return teacherRepository.findById(id).orElse(null);

    }

    /**
     * 按名字进行查询 Like
     * @param name 字符
     * @return
     */
    public List<Teacher> findByName(String name){


        return teacherRepository.findByNameLike(name);

    }

    /**
     * 增加
     */
    public Teacher add(Teacher teacher){

        return teacherRepository.save(teacher);
    }

    /** 更新老师
     * @param teacher
     * @return
     */
    public Teacher update(Teacher teacher){
        return teacherRepository.save(teacher);
    }

    /**
     * 删除老师
     * @param id
     */
    public void delete(Integer id){
        teacherRepository.deleteById(id);
    }

}

